package www.reagnetbox.com.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import www.reagnetbox.com.R;
import www.reagnetbox.com.adapter.UserListAdapter;
import www.reagnetbox.com.bean.User;
import www.reagnetbox.com.util.LitePalUtils;
import www.reagnetbox.com.util.RecyclerViewUtil;
import www.reagnetbox.com.util.XPopupUtil;
import www.reagnetbox.com.view.EditUserPopup;

public class UserManageActivity extends AppCompatActivity implements View.OnClickListener {

    private UserListAdapter userListAdapter;
    private Context context = this;
    private List<User> allUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_manage);
        ImmersionBar.with(this).init();
        initView();
        refreshData();
    }

    private void initView() {
        initTopButtonViews();
        initQueryListViews();
    }

    private void initTopButtonViews() {
        findViewById(R.id.tv_add_user).setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);
    }

    private void initQueryListViews() {
        userListAdapter = new UserListAdapter(this,R.layout.item_user_list, allUsers);
        userListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            User user = allUsers.get(position);
            switch (view.getId()){
                case R.id.tv_edit_info:
                    XPopupUtil.showCustomPopup(context,new EditUserPopup(context,user ,true, this::refreshData));
                    break;
                case R.id.tv_update_pwd:
                    XPopupUtil.showCustomPopup(context,new EditUserPopup(context,user , false,this::refreshData));
                    break;
                case R.id.tv_del:
                    XPopupUtil.showConfirmPopup(context, "删除用户", "确定删除该用户？", () -> {
                        LitePalUtils.delUser(user.getId());
                        refreshData();
                    });
                    break;
            }
        });
        RecyclerViewUtil.initRecyclerView(context, userListAdapter);
    }

    private void refreshData(){
        allUsers = LitePalUtils.findAllUsers();
        if (allUsers!=null){
            userListAdapter.setNewData(LitePalUtils.findAllUsers());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.with(this).destroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_add_user:
                XPopupUtil.showCustomPopup(context,new EditUserPopup(context,null,null, this::refreshData));
                break;
            case R.id.tv_back:
                finish();
                break;
        }
    }
}
